<?php
/*
 * Home page
 * Author: Bumoni
 * Created on :12/5/2011
 * Revision histor:
 */

include("includes/db.class.php");
include("includes/dbtransction.php");
include("includes/validate.php");

$db=new database; //initializing the database access class

/*ting the session*/
session_start();

/* Initilizaing variables*/
$fname='';
$email='';
$pwd='';
$link='';
/* if signup form is posted */
if ($_POST) 
{
    $error=array();
    $val=new validate; //initializing the validation class
    
    $fname=$_POST['fName'];  //catching the fname 
    
    /* validating fname*/
    $val->fname=$fname;
    $flag=$val->validate_gen();
    if($flag==2)
        $error['fname']="fname";
   
    $email=$_POST['email'];  //catching the email 
    
    /* validating email*/
    $val->email=$email;
    $flag=$val->validate_email();
    if($flag==2)
        $error['email']="email";
    
    $pwd=$_POST['pwd']; //catching password
    
    /* validating password*/
    $val->pwd=$pwd;
    $flag=$val->validate_pwd();
    if($flag==2)
        $error['pwd']="pwd";
    
    $link=$_POST['link']; //catching link
    
    /* validating link*/
    $val->link=$link;
    $flag=$val->validate_link();
    if($flag==2)
        $error['link']="link";
    if(count($error)==0)
    {
        $ins=new dbtransction;  //initializing the class
        
        $ins->fname=$fname;
        $ins->email=$email;
        $ins->pwd=$pwd;
        $ins->link=$link;
        $uid=$ins->insert_user_normal();  //calling the insert method the method returns the user id of the newly created user
        if($uid)
        {
            /*loding the id to the session*/
            $_SESSION['id']=$uid;
            $_SESSION['theme']=1;
             header( 'Location:themes.php') ;
        }
        else
        {
            echo "redirect to error page";
        }
    }
    else
    {
        echo "error";
    }
}
?>
<html>
    <head>
        <title>Card - Home</title>
        <link rel=StyleSheet href="css/header.css" type="text/css" media=screen></link>
        <link rel=StyleSheet href="css/content.css" type="text/css" media=screen></link>
    </head>
    <body>
<?php
include('includes/header.php');
?>
        <div class="rootBody">
            <div class="leftBody">
                <!-- Registration form begins -->

                <form method="post" action="index.php">
                    Signup form
                    <br />
                    Full Name:
                    <br />
                    <input type="text" name="fName" value="<?php echo $fname;?>" />
                    <span> 
                        <?php
                            if($error['fname']=='fname')
                            {
                                echo "Full Name is empty";
                            }
                        ?>
                    </span>
                    <br />
                    Enter email address:
                    <br />
                    <input type="text" name="email" value="<?php echo $email;?>" />
                    <span>
                        <?php
                            if($error['email']=='email')
                            {
                                echo "Email is invalid or email already exists";
                            }
                        ?>
                    </span>
                    <br />
                    Password:
                    <br />
                    <input type="password" name="pwd" value="<?php echo $pwd;?>" />
                    
                    <span>
                        <?php
                            if($error['pwd']=='pwd')
                            {
                                echo "Password is not valid";
                            }
                        ?>
                    </span>
                    <br />
                    Pick Up a link:
                    <br />
                    <input type="text" name="link" value="<?php echo $link;?>" />
                    <?php
                            if($error['link']=='link')
                            {
                                echo "Link already exists";
                            }
                        ?>
                    <br />
                    <input type="submit" value="Create" name="sbottom" />
                </form>

                <!-- End -->
            </div>
            <div class="rightBody">
                left part 
            </div>
            <br style="clear: both;" />
        </div>
    </body>
</html>

